<script setup>
import { ref } from 'vue'
import { Delete } from '@element-plus/icons-vue'
import {
  getOrderListByQueryinfService,
  delPurchaseOrderByIdService
} from '@/api/purchaseManage.js'
import { usePurchaserStore } from '@/stores'
import { ElInput } from 'element-plus'
import { useRouter } from 'vue-router'
const list = ref([])
const loading = ref(false)
const queryinf = ref('')
const isQuery = ref(false)
const purchaserStore = usePurchaserStore()
const router = useRouter()

const getlList = async () => {
  isQuery.value = true
  loading.value = true

  const res = await getOrderListByQueryinfService(queryinf.value)
  list.value = res.data.data
  loading.value = false
}

const onDel = async (row) => {
  await ElMessageBox.confirm('你确认要删除该采购订单么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await delPurchaseOrderByIdService(row.id)
  ElMessage.success('删除成功')
  getlList()
}

const checkOrder = (row) => {
  purchaserStore.setOrder(row)
  router.push('/purchaser/purchaseManage/orderDetails')
}

const onAdd = () => {
  router.push('/purchaser/purchaseManage/addOrder')
}
</script>

<template>
  <page-container title="采购订单管理">
    <template #extra>
      <ElInput v-model="queryinf" clearable style="width: 200px"></ElInput>
      <el-button @click="getlList">查询采购订单</el-button>
      <el-button @click="onAdd">创建采购订单</el-button>
    </template>

    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="orderId" label="订单号"></el-table-column>
      <el-table-column prop="supplierName" label="供应商名称"></el-table-column>
      <el-table-column prop="productName" label="产品名称"></el-table-column>
      <el-table-column prop="quantity" label="数量"></el-table-column>
      <el-table-column prop="unitPrice" label="采购单价"></el-table-column>
      <el-table-column prop="orderDate" label="采购日期"></el-table-column>
      <el-table-column prop="status" label="订单状态"></el-table-column>
      <el-table-column label="操作" width="200">
        <!-- row 就是 channelList 的一项， $index 下标 -->
        <template #default="{ row, $index }">
          <el-button plain type="primary" @click="checkOrder(row, $index)"
            >查看订单及入库详情</el-button
          >
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="没有采购订单" v-if="isQuery === true"></el-empty>
        <el-empty
          description="请输入供应商名称或所需产品的关键字查询采购订单"
          v-if="isQuery === false"
        ></el-empty>
      </template>
    </el-table>
  </page-container>
</template>

<style lang="scss" scoped></style>
